<?php

class Add_Publish_Article_ShowForm
{
    protected $_userId;
    protected $_articleId;
    
    public function __construct($articleId)
    {
        $userId = Ext_Auth_Front::getInstance()->getIdentity();
        
        if(!$articleId){
            $userProfile = App_Model_DbTable_User_Profile::getInstance()->getRowByUserId($userId);
            if(!$userProfile){
                require_once 'Ext/Publish/Article/Exception.php';
                throw new Ext_Publish_Article_Exception('User profile record not found.');
            }
            $lastArticleId = $userProfile['lastArticleId'];

            if(!$lastArticleId){
                $row = array(
                    'userId'  => $userId,
                );
                $lastArticleId = App_Model_DbTable_Article::getInstance()->addNew($row);
            
                $row = array(
                    'lastArticleId'             => $lastArticleId,
                );
                App_Model_DbTable_User_Profile::getInstance()->addOld($row, $userId);
            }
            
            $articleId = $lastArticleId;
        }
        
        if(!$articleId){
            require_once 'Add/Publish/Article/ShowForm/Exception.php';
            throw new Add_Publish_Article_ShowForm_Exception('Article record not found.');
        }
        $article = App_Model_DbTable_Article::getInstance()->getRowById($articleId);
        if(!$article){
            require_once 'Add/Publish/Article/ShowForm/Exception.php';
            throw new Add_Publish_Article_ShowForm_Exception('Article record not found.');
        }
        
        $this->_userId             = $article['userId'];
        $this->_articleId          = $articleId;
    }
    
    public function getArticleId()
    {
        return $this->_articleId;
    }

    public function buildTitle()
    {
        $articleRow = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        return $articleRow['title'];
    }

    public function buildAttachments()
    {
        $attachments = array();

        $articleRow = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        if(!$articleRow)
        {
            return $attachments;
        }
        
        $orderArray = unserialize($articleRow['attachments']);
        for($i=0;$i<count($orderArray);$i++)
        {
            $attachmentType   = $orderArray[$i]['type'];
            $attachmentDataId = $orderArray[$i]['id'];

            $attachment = new Ext_Article_Show_Attachment($attachmentType, $attachmentDataId);

            $attachments[] = $attachment;
        }
        
        return $attachments;
    }
    
    public function buildDescription()
    {
        $articleDescRow = App_Model_DbTable_Article_Desc::getInstance()->getRowByArticleId($this->_articleId);
        if(!$articleDescRow){
            return "";
        }
        
        return $articleDescRow['content'];
    }
    
    public function buildRecommendTags()
    {
        $_articleTags = array();
        if($this->_articleId)
        {
            $_articleTagRow = App_Model_DbTable_Article_Tags::getInstance()->getRowById($this->_articleId);
            if(isset($_articleTagRow['words']))
            {
                $_articleTags = explode(";", $_articleTagRow['words']);
            }
        }

        $recommendTags = App_Model_DbTable_Recommend_Tag::getInstance()->getAll();
        for($i=0;$i<count($recommendTags);$i++)
        {
            $_tagId = $recommendTags[$i]['tagId'];
            
            $_tag = new Add_Publish_Article_ShowForm_Tags($_tagId);
            if(in_array($_tag->name, $_articleTags))
            {
                $_tag->setChecked();
            }
            
            $recommendTags[$i] = $_tag;
        }

        return $recommendTags;
    }
    
    public function buildDefinedTags()
    {
        $_articleTags = $_delArticleTags = $_definedTagAll = array();
        if($this->_articleId)
        {
            $_articleTagRow = App_Model_DbTable_Article_Tags::getInstance()->getRowById($this->_articleId);
            if(isset($_articleTagRow['words']))
            {
                $_articleTags = explode(";", $_articleTagRow['words']);
            }
        }
        $recommendTagAll = App_Model_DbTable_Recommend_Tag::getInstance()->getAll();

        for($i=0;$i<count($recommendTagAll);$i++)
        {
            $_tagId = $recommendTagAll[$i]['tagId'];
            
            $_tagRow = App_Model_DbTable_Tags::getInstance()->getRowById($_tagId);
            if(in_array($_tagRow['tagName'], $_articleTags))
            {
                $_delArticleTags[] = $_tagRow['tagName'];
            }
        }
        for($i=0;$i<count($_articleTags);$i++)
        {
            $_tagName = $_articleTags[$i];
            
            if(!$_tagName)
            {
                continue;
            }
            
            if(in_array($_tagName, $_delArticleTags))
            {
                continue;
            }
            
            $_definedTagAll[] = $_tagName;
        }
        
        $definedTags = join("\n", $_definedTagAll);
        
        return $definedTags;
    }
    

}
